package com.example.demonettyserver;

import com.heyi.nettylong.common.MessageHandler;
import com.heyi.nettylong.server.listener.ChannelDataListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author dengfeng
 * @date 2022/12/618:01
 */
@Slf4j
@Component
public class NettyServerListener implements ChannelDataListener {
    @Override
    public boolean authority(String clientId, long timesnamp, String sign) {
        log.info("新增了[{}]客户端", clientId);
        return true;
    }

    @Override
    public void receiveData(MessageHandler messageHandler) throws Exception {
        log.info("收到数据：{}--{}--{}", messageHandler.getMessage().getClientId(), messageHandler.getMessage().getMsgId(), messageHandler.getMessage().getData());
        if(messageHandler.getMessage().getMsgId() > 0) {
            // 自动回复
            messageHandler.sendMessage("回复:" + messageHandler.getMessage().getData());
        }
    }

    @Override
    public void disconnected(String clientId, Throwable cause) {

    }
}
